package session.stateless;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.sql.Timestamp;
import java.util.List;
import javax.persistence.Query;
import entity.Supplier;
import entity.Employee;
import entity.MaterialRequired;
import entity.MaterialRequirementPlan;
import entity.RawMaterial;
import entity.RawMaterialDelivery;
import java.util.ArrayList;
import entity.RawMaterialPurchaseOrder;
import entity.WarehouseZone;

@Stateless
public class RawMaterialPurchaseOrderSessionBean implements RawMaterialPurchaseOrderSessionBeanLocal {

    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public MaterialRequirementPlan getMRP(Long mrpId) {
        MaterialRequirementPlan mrp = entityManager.find(MaterialRequirementPlan.class, mrpId);
        
        return mrp;
    }
    
    @Override
    public void updateMRP(MaterialRequirementPlan mrp) {
        MaterialRequirementPlan temp = entityManager.find(MaterialRequirementPlan.class, mrp.getId());
        
        temp.setWeight(mrp.getWeight());
        temp.setStatus(mrp.getStatus());
        
        entityManager.persist(temp);
        entityManager.flush();
    }

    @Override
    public List<RawMaterialDelivery> getRMDs() {
        Query query = entityManager.createQuery("SELECT r FROM RawMaterialDelivery r");

        ArrayList<RawMaterialDelivery> rawMaterialDeliveries = new ArrayList<RawMaterialDelivery>();

        for (Object o : query.getResultList()) {
            RawMaterialDelivery rmd = (RawMaterialDelivery) o;

            rawMaterialDeliveries.add(rmd);
        }

        return rawMaterialDeliveries;
    }

    @Override
    public void addRawMaterialDelivery(RawMaterialDelivery rmd) {
        RawMaterial rm = entityManager.find(RawMaterial.class, rmd.getMaterialRequired().getRawMaterial().getId());

        MaterialRequired mr = rmd.getMaterialRequired();
        mr.setRawMaterial(rm);

        Employee e = entityManager.find(Employee.class, rmd.getWarehouseRep().getUserId());

        Supplier s = entityManager.find(Supplier.class, rmd.getSupplier().getSupplierId());

        RawMaterialPurchaseOrder rmpo = entityManager.find(RawMaterialPurchaseOrder.class, rmd.getRawMaterialPurchaseOrder().getId());

        WarehouseZone wz = entityManager.find(WarehouseZone.class, rmd.getWarehouseZone().getId());

        rmd.setWarehouseZone(wz);
        rmd.setMaterialRequired(mr);
        rmd.setSupplier(s);
        rmd.setWarehouseRep(e);
        rmd.setRawMaterialPurchaseOrder(rmpo);

        entityManager.persist(rmd);
        entityManager.flush();
    }

    @Override
    public List<RawMaterialDelivery> getRMDsByRMPO(Long rmpoId) {
        Query query = entityManager.createQuery("SELECT r FROM RawMaterialDelivery r");

        ArrayList<RawMaterialDelivery> rawMaterialDeliveries = new ArrayList<RawMaterialDelivery>();

        for (Object o : query.getResultList()) {
            RawMaterialDelivery rmd = (RawMaterialDelivery) o;

            if (rmd.getRawMaterialPurchaseOrder().getId().compareTo(rmpoId) == 0) {
                rawMaterialDeliveries.add(rmd);
            }
        }

        return rawMaterialDeliveries;
    }

    @Override
    public List<RawMaterialPurchaseOrder> getRMPOsByStatus(String status) {
        Query query = entityManager.createQuery("SELECT r FROM RawMaterialPurchaseOrder r");

        ArrayList<RawMaterialPurchaseOrder> rawMaterialPurchaseOrders = new ArrayList<RawMaterialPurchaseOrder>();

        for (Object o : query.getResultList()) {
            RawMaterialPurchaseOrder rmpo = (RawMaterialPurchaseOrder) o;

            if (rmpo.getStatus().equals(status)) {
                rawMaterialPurchaseOrders.add(rmpo);
            }
        }

        return rawMaterialPurchaseOrders;
    }

    @Override
    public void updateRMPO(Long rmpoId) {
        RawMaterialPurchaseOrder rmpo = entityManager.find(RawMaterialPurchaseOrder.class, rmpoId);

        rmpo.setStatus("Paid");

        entityManager.persist(rmpo);
        entityManager.flush();
    }

    @Override
    public List<RawMaterialPurchaseOrder> getRawMaterialPOs() {
        Query query = entityManager.createQuery("SELECT r FROM RawMaterialPurchaseOrder r");

        ArrayList<RawMaterialPurchaseOrder> rawMaterialPurchaseOrders = new ArrayList<RawMaterialPurchaseOrder>();

        for (Object o : query.getResultList()) {
            RawMaterialPurchaseOrder rmpo = (RawMaterialPurchaseOrder) o;

            rawMaterialPurchaseOrders.add(rmpo);
        }

        return rawMaterialPurchaseOrders;
    }

    @Override
    public void addRawMaterialPO(Timestamp deliveryDate, String remark, String userId, Supplier supplier,
            List<MaterialRequirementPlan> materialRequirementPlans, Double amountToPay) {
        Employee e = entityManager.find(Employee.class, userId);
        Supplier s = entityManager.find(Supplier.class, supplier.getSupplierId());

        List<MaterialRequirementPlan> temp = new ArrayList<MaterialRequirementPlan>();
        for (MaterialRequirementPlan mrp : materialRequirementPlans) {
            MaterialRequirementPlan temp2 = entityManager.find(MaterialRequirementPlan.class, mrp.getId());

            temp.add(temp2);
        }

        RawMaterialPurchaseOrder rmpo = new RawMaterialPurchaseOrder(deliveryDate, remark, e, s, temp, amountToPay);

        entityManager.persist(rmpo);
        entityManager.flush();
    }

    @Override
    public List<MaterialRequirementPlan> getMaterialRequirementPlans() {
        Query query = entityManager.createQuery("SELECT mrp FROM MaterialRequirementPlan mrp ORDER BY mrp.orderDate");

        ArrayList<MaterialRequirementPlan> materialRequirementPlans = new ArrayList<MaterialRequirementPlan>();

        for (Object o : query.getResultList()) {
            MaterialRequirementPlan mrplans = (MaterialRequirementPlan) o;
            materialRequirementPlans.add(mrplans);
        }

        return materialRequirementPlans;
    }
}
